# Copyright (c) 2008 Alvin Wang http://cloudwizard.com/
# License New BSD
# See README for additional information
"""
Amazon Web Services Cloud Support

"""

from cloudwizard.cloud import Cloud
from cloudwizard.config import Config
import boto
from api import GoGridClient

class GoGrid(Cloud):
    """GoGrid Cloud Api
    """
    cloud_name = "GoGrid - Part of Servepath"
    cloud_abbrev = "gogrid"
    
    def __init__(self, username='default', directory=None, autoload=True):
        if autoload:
            self.cloud_auth = Config( cloud_abbrev=self.cloud_abbrev, \
                type='user', config_name=username, autoload=True) 
            self.cloud_obj = GoGridClient(key=self.cloud_auth.data['apikey'],
                secret=self.cloud_auth.data['apisecret'])        
            
    def script_auth(self, username='default'):
        self.cloud_auth = Config( cloud_abbrev=self.cloud_abbrev, type='user',
            autoload=False) 
        
        self.cloud_auth.prompt(question="Please enter your API Key?", varname="apikey")
            
        self.cloud_auth.prompt(question="Please enter your Shared Secret Key?", 
            varname="apisecret")
         
        username = self.cloud_auth.prompt(question="What user name would you like to save this?",
            default='default', vartype="username", varname="username")
        # Add more stuff
        # get a default key or create it if necessary
        # 
        self.cloud_auth.write_config( config_name=username)
        
    def get_service(self, service=None, **args):
        """return a AWS service"""
        # could use lambda but it is more painful
        if service == "compute":
            return Ec2( auth=self.cloud_auth)
        if service == "ec2":
            return Ec2( auth=self.cloud_auth)
        if service == "queue":
            return Sqs( auth=self.cloud_auth)
        if service == "sqs":
            return Sqs( auth=self.cloud_auth)
        if service == "s3":
            return Sqs( auth=self.cloud_auth)
        # error?

    def get_auth(self, service=None, **args):
        """return a AWS service"""
        # could use lambda but it is more painful
        return self.cloud_userid,self.cloud_password

    def gogrid_billing(self):
        """Get billing info"""
        result = self.cloud_obj.request("myaccount/billing/get")
        return result['list']

    def gogrid_password_list(self):
        """Get password list"""
        result = self.cloud_obj.request("support/password/list")
        return result['list']

    def gogrid_password_get(self, id=None):
        """Get password by Id"""
        result = self.cloud_obj.request("support/password/get",params={'id':id})        
        return result['list']

    def gogrid_lookups(self, lookup='lookups'):
        """Lit valid lookups"""
        if lookup=='all':
            lookup='lookups'
        result = self.cloud_obj.request("common/lookup/list",params={'lookup':lookup})        
        return result['list']
